<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Article.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Article.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Articles.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "ArticleRevision.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "ArticleRevisions.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "TagItemAssoc.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "TagItemAssocs.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Users.php";

require_once '../library/Utility/Converter.php';
require_once '../library/Utility/TimeHelper.php';

class Admin_ArticleController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $articleModel;
    private $articleRevisionModel;
    private $tagItemAssocModel;
    
    private $modelUser;
    private $currentUser;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////    
    public function init() {
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        
        $this->articleModel = new Application_Models_Articles();
        $this->articleRevisionModel = new Application_Models_Article_Revisions();
        $this->tagItemAssocModel = new Application_Model_Tag_Item_Assocs();
        
        $this->modelUser = new End_Models_Users();
        
        //OTHER 
        $this->currentUser = $this->getInforUser();
        
        //VIEW 
        $this->view->currentUser = $this->currentUser;  
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    ////////////////////   
    public function indexAction() {

        $articles = $this->articleModel->fetchAll();
        $this->view->articles = $articles;
    }

    public function addAction() {
        $form = new Application_Forms_Article();
        $form->submit->setLabel('Save');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /* Input value */

                $article_id = $form->getValue('article_id');
                $category_id = $form->getValue('category_id');
                $title = $form->getValue('title');
                $sub_title = $form->getValue('sub_title');
                $slug = '';
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $author = $form->getValue('author');
                $icons = '';
                $image_square = '';
                $image_thumbnail = '';
                $image_small = '';
                $image_crop = '';
                $image_medium = '';
                $image_large = '';
                $status = 'inactive';
                $num_views = 0;
                $created_date = Utility_Time_Helper::getCurrentTime();
                $created_user_id = $form->getValue('created_user_id');
                if ($created_user_id == "")
                    $created_user_id = null;
                // Need user module.
                $created_user_name = $form->getValue('created_user_name');
                $updated_date = null;
                $updated_user_id = null;
                $updated_user_id = null;
                $updated_user_name = null;
                $activate_user_id = null;
                $activate_user_name = null;
                $activate_date = null;
                $allow_comment = $form->getValue('allow_comment');
                $num_comments = 0;
                $is_hot = $form->getValue('is_hot');
                $show_date = null;
                $sticky = $form->getValue('sticky');
                $language = $form->getValue('language');

                /* Create new article model */

                $article = new Article(
                                $article_id,
                                $category_id,
                                $title,
                                $sub_title,
                                $slug,
                                $description,
                                $content,
                                $author,
                                $icons,
                                $image_square,
                                $image_thumbnail,
                                $image_small,
                                $image_crop,
                                $image_medium,
                                $image_large,
                                $status,
                                $num_views,
                                $created_date,
                                $created_user_id,
                                $created_user_name,
                                $updated_date,
                                $updated_user_id,
                                $updated_user_name,
                                $activate_user_id,
                                $activate_user_name,
                                $activate_date,
                                $allow_comment,
                                $num_comments,
                                $is_hot,
                                $show_date,
                                $sticky,
                                $language);
                $inserted_id = $this->articleModel->_insert($article);

                /* Add tag assoc */

                $tags = $form->getValue('tag');
                $this->InsertTags($tags, $inserted_id);

                /* Create first revision for this article */

                $article_revision = new Article_Revision(
                                $inserted_id,
                                $category_id,
                                $title,
                                $sub_title,
                                $slug,
                                $description,
                                $content,
                                $author,
                                $icons,
                                $created_date,
                                $created_user_id,
                                $created_user_name);
                $this->articleRevisionModel->_insert($article_revision);

                // Redirect after insert.
                $param = array("article_id" => $inserted_id);
                $this->_helper->redirector("edit", "article", "admin", $param);
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $form = new Application_Forms_Article();
        //$form->submit->setLabel('Đồng ý');
        //$form->Revision->setLabel('Lược sử');
        //$form->Preview->setLabel('Xem trước');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            /* User get revision? */

            $revision = $this->getRequest()->getPost('Revision');
            if ($revision == 'Revision') { // user request revision
                $article_id = $this->getRequest()->getPost('article_id');
                $param = array("article_id" => $article_id);
                $this->_helper->redirector("index", "articlerevision", "admin", $param);
                return;
            }

            /* User preview */

            /* User save data */

            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /* Input value */

                $article_id = $form->getValue('article_id');
                $category_id = $form->getValue('category_id');
                $title = $form->getValue('title');
                $sub_title = $form->getValue('sub_title');
                $slug = '';
                $description = $form->getValue('description');
                $content = $form->getValue('content');
                $author = $form->getValue('author');
                $icons = '';
                $image_square = '';
                $image_thumbnail = '';
                $image_small = '';
                $image_crop = '';
                $image_medium = '';
                $image_large = '';
                $status = $form->getValue('status');
                $num_views = $form->getValue('num_views');
                $created_date = $form->getValue('created_date');
                $created_user_id = $form->getValue('created_user_id');
                if ($created_user_id == "")
                    $created_user_id = null;
                $created_user_name = $form->getValue('created_user_name');
                $updated_date = Utility_Time_Helper::getCurrentTime();
                $updated_user_id = $form->getValue('updated_user_id');
                if ($updated_user_id == "")
                    $updated_user_id = null;
                $updated_user_name = $form->getValue('updated_user_name');
                $activate_user_id = $form->getValue('activate_user_id');
                if ($activate_user_id == "")
                    $activate_user_id = null;
                $activate_user_name = $form->getValue('activate_user_name');
                $activate_date = $form->getValue('activate_date');
                if ($activate_date == "")
                    $activate_date = null;
                $allow_comment = $form->getValue('allow_comment');
                $num_comments = $form->getValue('num_comments');
                $is_hot = $form->getValue('is_hot');
                $show_date = $form->getValue('show_date');
                if ($show_date == "")
                    $show_date = null;
                $sticky = $form->getValue('sticky');
                $language = $form->getValue('language');

                /* Create new article model */

                $article = new Article(
                                $article_id,
                                $category_id,
                                $title,
                                $sub_title,
                                $slug,
                                $description,
                                $content,
                                $author,
                                $icons,
                                $image_square,
                                $image_thumbnail,
                                $image_small,
                                $image_crop,
                                $image_medium,
                                $image_large,
                                $status,
                                $num_views,
                                $created_date,
                                $created_user_id,
                                $created_user_name,
                                $updated_date,
                                $updated_user_id,
                                $updated_user_name,
                                $activate_user_id,
                                $activate_user_name,
                                $activate_date,
                                $allow_comment,
                                $num_comments,
                                $is_hot,
                                $show_date,
                                $sticky,
                                $language);

                $this->articleModel->_update($article);

                /* Update assoc tag */

                $tags = $form->getValue('tag');
                //
                // Delete assoc
                $this->tagItemAssocModel->DeleteArticles($article_id);
                //
                // Insert assoc tag
                $this->InsertTags($tags, $article_id);

                /* Create revision for edited article */

                $article_revision = new Article_Revision(
                                $article_id,
                                $category_id,
                                $title,
                                $sub_title,
                                $slug,
                                $description,
                                $content,
                                $author,
                                $icons,
                                $created_date,
                                $created_user_id,
                                $created_user_name);
                $this->articleRevisionModel->_insert($article_revision);
            } else {
                $form->populate($formData);
            }
        } else {
            $article_id = $this->_getParam('article_id');
            if ($article_id > 0) {
                $articleArray = Utility_Converter::objectToArray($this->articleModel->_find($article_id));
                $form->populate($articleArray);
            }
        }
    }

    public function deleteAction() {
        $article_id = $this->_getParam('article_id');

        /* Delete assoc */

        $this->tagItemAssocModel->DeleteArticles($article_id);

        /* Delete article */

        $this->articleModel->_delete($article_id);


        $this->_helper->redirector('index');
    }

    public function activeAction() {
        $article_id = $this->_getParam('article_id');
        $article = $this->articleModel->_find($article_id);
        $article->status = "active";
        $this->articleModel->_update($article);
        $this->_helper->redirector('index');
    }

    public function deactiveAction() {
        $article_id = $this->_getParam('article_id');
        $article = $this->articleModel->_find($article_id);
        $article->status = "inactive";
        $this->articleModel->_update($article);
        $this->_helper->redirector('index');
    }

    /**
     * 
     * Insert tags to database
     * @param unknown_type $tag
     */
    private function InsertTags($tag, $article_id) {
        if(isset($tag))
        foreach ($tag as $t) {
            // Create tag_item_assoc model

            // Create tag_item_assoc member value

            $tag_id = $t;
            $item_id = $article_id;
            $item_name = "article_id";
            $route_name = "news_article_details";
            $details_route_name = "news_tag_article";
            $params = "article_id:" . $item_id; // ex: article_id:1
            // Create new object
            $tag_item_assoc_object = new Tag_Item_Assoc(
                            $tag_id,
                            $item_id,
                            $item_name,
                            $route_name,
                            $details_route_name,
                            $params);
            // Insert to database
            $this->tagItemAssocModel->_insert($tag_item_assoc_object);
        }
    }

}